package com.sftz.modules.minapp.seriesCouresDetail.module;

import java.text.ParseException;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.sftz.assets.enums.OrderTypeEnum;
import com.sftz.modules.minapp.seriesCouresDetail.form.SerUserForm;
import com.sftz.modules.minapp.seriesCouresDetail.service.SerirsecoursedetailViewService;
import com.sftz.modules.minapp.user.cache.RedissionUserCache;
import com.uxuexi.core.common.util.Util;

@IocBean
@At("/wx/seriousCourseDetail")
public class SerirsecoursedetailModule {

	private static final Log log = Logs.get();

	@Inject
	private SerirsecoursedetailViewService serirsecoursedetailViewService;

	/**
	 * 查询系列课程详情
	 */
	@At
	@Ok("json")
	public Object seriousCourseDetail(@Param("id") final Integer id, @Param("..") final SerUserForm form) {
		String token = form.getToken();
		Integer userId = RedissionUserCache.getUserId(token);
		return serirsecoursedetailViewService.serCouDetail(id, userId);

	}

	/**
	 * 查询当前是否有24小时之内未支付的此订单
	 */
	@At
	@Ok("json")
	public Object serOrderDetail(@Param("id") final Integer id, @Param("..") final SerUserForm form) {
		String token = form.getToken();
		Integer userId = RedissionUserCache.getUserId(token);
		return serirsecoursedetailViewService.serOrderDetail(id, userId);

	}

	/*
	 * 免费系列课程添加报名人数,用户商品表
	 * */
	@At
	@Ok("json")
	public Object addUserProduct(@Param("productId") final Integer productId,
			@Param("orderType") final Integer orderType, @Param("..") final SerUserForm form) throws ParseException {
		String token = form.getToken();
		Integer userId = RedissionUserCache.getUserId(token);
		if (!Util.isEmpty(userId)) {
			int i = serirsecoursedetailViewService.addUserProduct(productId, userId, orderType);
			if (i > 0) {
				if (orderType == OrderTypeEnum.COURSESERIES.intKey()) {
					serirsecoursedetailViewService.addNum(productId);
				}
				if (orderType == OrderTypeEnum.COURSE.intKey()) {
					serirsecoursedetailViewService.addCourseStudyCountNum(productId);
				}
			}

			return i;
		}
		return null;

	}

}